﻿using BarcodeStandard;
using SkiaSharp;
using StoreProject.Entity;
using StoreProject.Forms.SettingForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Type = BarcodeStandard.Type;

namespace StoreProject.Forms
{
    public partial class TestPrintForm : Form
    {
        private OrderEntity orderEntity;
        private List<WorkingProcedureEntity> workingProcedureEntities;
        public TestPrintForm()
        {
            InitializeComponent();
        }
        public TestPrintForm(OrderEntity orderEntity, List<WorkingProcedureEntity> workingProcedureEntities)
        {
            this.orderEntity = orderEntity;
            this.workingProcedureEntities = workingProcedureEntities;
            InitializeComponent();
            if (orderEntity != null && workingProcedureEntities != null && workingProcedureEntities.Count > 0)
            {
                using (Barcode barcode = new Barcode())
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        barcode.LabelFont = new SKFont(SKTypeface.FromFamilyName("微软雅黑"), 10, 1, 0);
                        barcode.IncludeLabel = true;
                        //barcode.IncludeLabel = false;
                        barcode.Alignment = AlignmentPositions.Center;
                        var barcodeImage = barcode.Encode(Type.Code128, orderEntity.OrderId, new SKColor(0, 0, 0, 255), new SKColor(240, 240, 240), 204, 45);
                        var aa = Image.FromStream(barcodeImage.Encode().AsStream());
                        this.pictureBox1.Image = aa;
                    }
                }
                if (!string.IsNullOrEmpty(orderEntity.OrderId))
                {
                    this.label3.Visible = true;
                    this.label3.Text = orderEntity.OrderId;
                }
                if (!string.IsNullOrEmpty(orderEntity.SemiFinishedProductName))
                {
                    this.label4.Visible = true;
                    this.label4.Text = orderEntity.SemiFinishedProductName;
                }
                if (!string.IsNullOrEmpty(orderEntity.LineType))
                {
                    this.label16.Visible = true;
                    this.label16.Text = orderEntity.LineType;
                }
                if (!string.IsNullOrEmpty(orderEntity.FinishedProductName))
                {
                    this.label9.Visible = true;
                    this.label9.Text = orderEntity.FinishedProductName;
                }
                if (!string.IsNullOrEmpty(orderEntity.PlanNums.ToString()))
                {
                    this.label8.Visible = true;
                    this.label8.Text = orderEntity.PlanNums.ToString();
                }
                if (!string.IsNullOrEmpty(orderEntity.Batch))
                {
                    this.label17.Visible = true;
                    this.label17.Text = orderEntity.Batch;
                }
                if (!string.IsNullOrEmpty(orderEntity.RequestedDate.ToString()))
                {
                    this.label10.Visible = true;
                    this.label10.Text = orderEntity.RequestedDate.ToString("yyyy-MM-dd");
                }
                if (!string.IsNullOrEmpty(orderEntity.Customer))
                {
                    this.label15.Visible = true;
                    this.label15.Text = orderEntity.Customer;
                }
                string temp = "";
                foreach (var item in workingProcedureEntities.OrderBy(x => x.IndexNum))
                {
                    temp += (item.IndexName + "+");
                }
                if (!string.IsNullOrEmpty(temp))
                    temp = temp.Remove(temp.Length - 1, 1);
                this.label19.Text = temp;
                workingProcedureEntities.Clear();
                for (int i = workingProcedureEntities.Count; i < 10; i++)
                {
                    workingProcedureEntities.Add(new WorkingProcedureEntity() { Id = Guid.NewGuid().ToString(), IndexNum = i + 1 });
                }
                dataGridView1.DataSource = workingProcedureEntities;

            }
        }
        public TestPrintForm(string name, string age)
        {
            InitializeComponent();


            this.label3.Text = name;
            this.label4.Text = age;
            this.dataGridView1.Visible = true;
            WorkingProcedureEntity entity = new WorkingProcedureEntity();
            entity.ProcessFlowId = "12312313";
            entity.Remark = "备注";
            entity.IndexName = "清洗";
            entity.IndexNum = 1;
            entity.CreateTime = DateTime.Now;
            entity.UpdateTime = DateTime.Now;
            WorkingProcedureEntity entity1 = new WorkingProcedureEntity();
            entity1.ProcessFlowId = "12312313";
            entity1.Remark = "备注";
            entity1.IndexName = "清洗";
            entity1.IndexNum = 1;
            entity1.CreateTime = DateTime.Now;
            entity1.UpdateTime = DateTime.Now;
            List<WorkingProcedureEntity> list = new List<WorkingProcedureEntity>();
            list.Add(entity);
            list.Add(entity1);
            dataGridView1.DataSource = list;

            using (Barcode barcode = new Barcode())
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    barcode.IncludeLabel = false;
                    barcode.Alignment = AlignmentPositions.Center;
                    var barcodeImage = barcode.Encode(Type.Code128, "038000356216", new SKColor(0, 0, 0, 255), new SKColor(255, 255, 255, 255), 204, 56);
                    var aa = Image.FromStream(barcodeImage.Encode().AsStream());
                    this.pictureBox1.Image = aa;
                }
            }

        }

        private void TestPrintForm_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.dataGridView1.ClearSelection();
        }
    }
}
